<?php namespace App\Http\Controllers\Api;

use App\ForumDiscuss;
use App\Http\Controllers\Controller;
use App\User;
use App\UserInfo;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;

class ForumController extends Controller
{
    public function setUserContent(Request $request,Guard $guard){
        if($guard->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        /** @var User $user */
        $user    = $guard->user();


        $userInfo =UserInfo::whereUserId($user->id)->first();
        if(!$userInfo)
            return response()->json(['level'=>'error','message'=>'请输入完善资料再评论']);
        if($userInfo->is_member==0)
            return response()->json(['level'=>'error','message'=>'非会员不能评论']);


        $fid     = $request->get("f_id");
        $pid     = $request->get("pid");
        $content = $request->get("content");

        if(is_null($content)||$content=="")
            return response()->json(['level'=>'error','message'=>'请输入内容']);


        $toUserId = 0;
        if(!is_null($pid) && $pid != 0){
            $thatDiscuss = ForumDiscuss::whereId($pid)->whereFId($fid)->first();
            if($thatDiscuss){
                $toUserId = $thatDiscuss->user_id;
            }else{
                $pid = 0;
            }
        }else{
            $pid = 0;
        }

        $newForumDiscuss = new ForumDiscuss();
        $newForumDiscuss->user_id    = $user->id;
        $newForumDiscuss->f_id       = $fid;
        $newForumDiscuss->to_user_id = $toUserId;
        $newForumDiscuss->pid        = $pid;
        $newForumDiscuss->content    = $content;
        $newForumDiscuss->save();

        return response()->json(['level'=>'success','message'=>trans('评论成功'),'data'=>$newForumDiscuss]);
    }
}